package com.dant.components.containers.panels
{
	import com.dant.events.MenuPanelEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.ControlBar;
	import mx.controls.Image;
	import mx.effects.Glow;
	import mx.effects.Parallel;
	import mx.effects.Resize;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;


	[Event(name="collapseWindow", type="mx.events.FlexEvent")]
	[Event(name="expandWindow", type="mx.events.FlexEvent")]
	[Event(name="collapsedWindow", type="mx.events.FlexEvent")]
	[Event(name="expandedWindow", type="mx.events.FlexEvent")]
	
    [Style(name="collapseButtonSkin",type="Class",inherit="no")]
    [Style(name="expandButtonSkin",type="Class",inherit="no")]
	public class MenuPanel extends AdvancedPanel
	{
		public static const CONST_DEFAULT_HEIGHT : Number = 250;
		public static const CONST_DEFAULT_WIDTH : Number = 200;
		
		public static const CONST_MIN_MENU_HEIGHT:Number = 100;
		public static const CONST_MIN_MENU_WIDTH:Number = 100;
		
		public var control_bar:ControlBar;
		
		public var collapseExpandButton:Image;
		
		private var _parallelEffect : Parallel;
		
		private var isCollapsed:Boolean = true;
		
		private var res:Resize;

		[Inspectable(type="Boolean")]
		protected var _canExtendAndCollapse:Boolean = true;
		private var canExtendAndCollapseChanged:Boolean = true;

		[Bindable]
//        [Embed(source="../assets/images/colapsar.png")]
        public var collapseIcon:Class;

        [Bindable]
//        [Embed(source="../assets/images/expandir.png")]
        public var expandIcon:Class;

        public function set canExtendAndCollapse (value:Boolean):void
        {
            _canExtendAndCollapse = value;
            canExtendAndCollapseChanged = true;
            invalidateProperties();
        }
        
        public function get canExtendAndCollapse ():Boolean
        {
            return (_canExtendAndCollapse);
        }

		public function MenuPanel ()
		{
			super( );
			
			_showMaxButton = false;
			_showMinButton = false;
		}


		override protected function createChildren() : void
		{
			super.createChildren();

			if ( (! minHeight) || (minHeight <= 0) )
			{
				this.minHeight = CONST_MIN_MENU_HEIGHT;
			}

			if ( (! minWidth) || (minWidth <= 0) )
			{
				this.minWidth = CONST_MIN_MENU_WIDTH;
			}

			if ( (! height) || (height < minHeight) )
			{
				this.height = CONST_DEFAULT_HEIGHT;
			}

			if ( (! width) || (width < minWidth) )
			{
				this.width = CONST_DEFAULT_WIDTH;
			}


			// skins the close button as a Minimize button
			if (showCloseButton)
			{
				skinCloseButton();
//				windowCloseButton.addEventListener(MouseEvent.MOUSE_DOWN, closePanelHandler);
			}
		}
		
		override protected function commitProperties():void
		{
		    super.commitProperties();
            // collapse expand button init
            if ( canExtendAndCollapseChanged )
            {
                if (canExtendAndCollapse)
                {
                    if ( ! collapseExpandButton )
                    {
                        collapseExpandButton = new Image ();
                        collapseExpandButton.source = collapseIcon;
                        collapseExpandButton.width = 16;
                        collapseExpandButton.height = 16;
                        
                        var glowImage : Glow = new Glow();
                        glowImage.duration = 500;
                        glowImage.alphaFrom = 1.0;
                        glowImage.alphaTo = 0.0;
                        glowImage.color =  0xFFFFFF;
                        
                        collapseExpandButton.setStyle("mouseDownEffect",glowImage);
                        
                        collapseExpandButton.addEventListener(MouseEvent.MOUSE_DOWN, 
                                                                but_collapseExpandMouseDownHandler,
                                                                false,0,true);
                    }
                    
                    if ( ! control_bar )
                    {
                        control_bar = new ControlBar();
                        control_bar.height = 30;
                        control_bar.addChild(collapseExpandButton);
                        
                    }

                    isCollapsed = false;
                    
                    setStyle("paddingBottom",0);
                    setStyle("paddingTop",0);
                    setStyle("paddingLeft",0);
                    setStyle("paddingRight",0);

                    addChild(control_bar);

                    addEventListener(MenuPanelEvent.COLLAPSE_WINDOW, collapseWindowHandler,false,0,true);
                    addEventListener(MenuPanelEvent.EXPAND_WINDOW,expandWindowHandler,false,0,true);

                    createComponentsFromDescriptors();
                }
                else
                {
                    removeEventListener(MenuPanelEvent.COLLAPSE_WINDOW, collapseWindowHandler);
                    removeEventListener(MenuPanelEvent.EXPAND_WINDOW, expandWindowHandler);
                    removeChild(control_bar);
                }
                canExtendAndCollapseChanged = false;
            }
		}

		override protected function skinCloseButton():void
		{
			windowCloseButton.setStyle("upSkin", getMinimizeWindowButtonSkin());
			windowCloseButton.setStyle("overSkin", getMinimizeWindowButtonOverSkin());
			windowCloseButton.setStyle("downSkin", getMinimizeWindowButtonDownSkin());
		}

        /**
         * Close panel event handler
         */
        override public function close ( ):void
        {
            dispatchCloseEvent();

            if ( ! isCollapsed )
            {
//              this.windowCloseButton.enabled = false;
                if ( ! res )
                {
                    res = new Resize ();
                }
                
                var newHeight:Number = titleBar.height + controlBar.height;
    
                res.heightFrom = height;
                res.heightTo = newHeight;
                res.target = this;
                if ((res.heightFrom != res.heightTo) && (!res.isPlaying))
                {
                    res.addEventListener(EffectEvent.EFFECT_END,resize_effectEndHandler);          
                    res.play();
                }
            }
            else
            {
                parent.removeChild(this);
                isCollapsed = false;
                collapseExpandButton.source = collapseIcon;
                dispatchClosedEvent( );
            }
        }

		public function but_collapseExpandMouseDownHandler ( event: MouseEvent ) :void
		{
			if (isCollapsed)
			{
				this.dispatchEvent(new MenuPanelEvent ( MenuPanelEvent.EXPAND_WINDOW ) );
				collapseExpandButton.source = collapseIcon;
			}
			else
			{
				this.dispatchEvent(new MenuPanelEvent ( MenuPanelEvent.COLLAPSE_WINDOW ) );
				collapseExpandButton.source = expandIcon;
			}
		}

		private function resize_effectEndHandler (event:EffectEvent):void
		{
			if (! res.isPlaying)
			{
				parent.removeChild(this);
				trace ("MENU REMOVED");
				windowCloseButton.enabled = true;			
				res.removeEventListener(EffectEvent.EFFECT_END,resize_effectEndHandler);		
				dispatchClosedEvent( );
			}
		}

		/**
		 * Collapse window handler
		 */
		private function collapseWindowHandler (event:Event):void
		{
			if (isCollapsed == false)
			{
				_restoreHeight = height;	
				_restoreWidth = width;
				var newHeight:int = 0;
				newHeight += titleBar.height + controlBar.height;
				
    		    // FIXME: Poner collapseEffect
				if ( getStyle("minimizeEffect") )
				{
					resizeEffect ( this, newHeight, width, collapseEffectEnded );
				}
				else
				{
					this.height = newHeight;
				}

				isCollapsed = true;
			}
		}

		private function resizeEffect ( target : Object, newHeight : Number, newWidth : Number, handlerFunction : Function ) : void
		{
			var resize:Resize = new Resize ( );
			resize.target = this;
			resize.heightTo = newHeight;
			resize.widthTo = newWidth;

//			resize.addEventListener(EffectEvent.EFFECT_END, collapseEffectEnded);
//				resize.addEventListener(EffectEvent.EFFECT_END, expandEffectEnded);
			resize.addEventListener(EffectEvent.EFFECT_END, handlerFunction,false,0,true);

			resize.play( [this] );
		}

		private function expandWindowHandler (event:Event):void
		{
		    // FIXME: Poner expandEffect
			if (isCollapsed == true)
			{
				if ( getStyle("restoreEffect") )
				{
					resizeEffect(this,_restoreHeight,this.width, expandEffectEnded);
				}
				else
				{
					height = _restoreHeight;	
					width = _restoreWidth;
				}

				isCollapsed = false;
			}			
		}

		private function expandEffectEnded ( event : EffectEvent ) : void
		{
			dispatchEvent(new MenuPanelEvent(MenuPanelEvent.EXPANDED_WINDOW));
		}

		private function collapseEffectEnded ( event : EffectEvent ) : void
		{
			dispatchEvent(new MenuPanelEvent(MenuPanelEvent.COLLAPSED_WINDOW));
		}
	}
}